/* What???There seems to be nothing with a clear mind... */
#include <bits/stdc++.h>
using namespace std;

using LL=long long;
LL C[200000], A[200000];
LL T, n, ans;

void solve(){
    ans = 0;
    LL a = 0, b = 0, c = 0;
    LL *s[3] = {&a, &b, &c}, *x;
    cin>> n;
    memset(C, 0, 200000*sizeof(LL));
    for (LL i=0; i<n; i++){
        cin>> A[i];
        x = s[0];
        s[0] = s[1];
        s[1] = s[2];
        if (a == 0){
            a = A[i];
            C[i] = a==b?a:(a==c?a:0);
            s[2] = &a;
        }
        else if (b == 0){
            b = A[i];
            C[i] = b==a?b:(b==c?b:0);
            s[2] = &b;
        }
        else if (c == 0){
            c = A[i];
            C[i] = c==a?c:(c==b?c:0);
            s[2] = &c;
        }
        else{
            if (A[i] == a){
                C[i] = a;
            }else if (A[i] == b){
                C[i] = b;
            }else if (A[i] == c){
                C[i] = c;
            }
            *x = A[i];
            s[2] = x;
        }
        ans += C[i];
    }
    cout<< ans<< endl;
}

int main(){
    auto _1 = freopen("color.out", "w", stdout);
    auto _2 = freopen("color.in" , "r", stdin );
    cin>> T;
    for (LL i=0; i<T; i++)
        solve();
    return 0;
}
